package com.yootk.servlet;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Servlet生命周期
 */
@WebServlet("/life")
/*@WebServlet(value = "/life", loadOnStartup = 1, initParams = {
    @WebInitParam(name = "message", value = "www.yootk.com")
})*/
public class LifeCycleServlet extends HttpServlet {

    public LifeCycleServlet() {
        System.out.println("【LifeCycleServlet.()】构造方法。");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("【LifeCycleServlet.init()】Servlet初始化。");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        // 此时要通过Servlet的配置获取指定名称的初始化参数内容
        System.out.println("【LifeCycleServlet.init(ServletConfig config)】Servlet初始化，message = " + config.getInitParameter("message"));
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("【LifeCycleServlet.service()】处理用户HTTP请求。");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("【LifeCycleServlet.doGet()】处理GET请求。");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("【LifeCycleServlet.doPost()】处理POST请求。");
    }

    @Override
    public void destroy() {
        System.out.println("【LifeCycleServlet.destroy()】Servlet销毁。");
    }
}
